package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController("adminShopController")
@Slf4j
@Api(tags = "商店操作")
@RequestMapping("/admin/shop")
public class ShopController {

    @Autowired
    ShopService shopService;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    private final String key = "SHOP_STATUS";

    @PutMapping("/{status}")
    @ApiOperation("设置店铺状态")
    public Result<Void> setShopStatus(@PathVariable Integer status) {
        log.info("修改店铺状态 status: {}", status == 1 ? "营业中" : "打烊中");
        redisTemplate.opsForValue().set(key, status);
        return Result.success();
    }

    @GetMapping("/status")
    @ApiOperation("获得店铺状态")
    public Result<Integer> getShopStatus() {

        Integer status = (Integer) redisTemplate.opsForValue().get(key);
        if(status == null){
            throw new NullPointerException("店铺status不存在");
        }
        log.info("获得店铺营业状态 {}", status == 1 ? "营业中" : "打烊中");
        return Result.success(status);
    }


}
